package com.heu.blood.bloodtest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.bloodtest.dao.BloodSampleDao;
import com.heu.blood.bloodtest.entity.BloodSampleEntity;
import com.heu.blood.bloodtest.service.BloodSampleService;
import com.heu.blood.bloodtest.vo.BloodSampleReportVo;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Map;


@Service("bloodSampleService")
public class BloodSampleServiceImpl extends ServiceImpl<BloodSampleDao, BloodSampleEntity> implements BloodSampleService {

    @Autowired
    private BloodSampleDao bloodSampleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        BloodSampleEntity bloodSample = new BloodSampleEntity();
        if(Strings.isNotBlank((String) params.get("bloodSampleId")))
            bloodSample.setBloodSampleId((String)params.get("bloodSampleId"));
        if(Strings.isNotBlank((String) params.get("bloodId")))
            bloodSample.setBloodId((String)params.get("bloodId"));
        if(Strings.isNotBlank((String) params.get("sampleStatus")))
            bloodSample.setSampleStatus((String)params.get("sampleStatus"));
        if(Strings.isNotBlank((String) params.get("place")))
            bloodSample.setPlace((String)params.get("place"));

        //添加时间范围条件
        String receivedStartTime = null;
        String receivedEndTime = null;
        if(Strings.isNotBlank((String)params.get("receivedStartTime")))
            receivedStartTime = (String)params.get("receivedStartTime");
        if(Strings.isNotBlank((String)params.get("receivedEndTime")))
            receivedEndTime = (String)params.get("receivedEndTime");

        //创建构造器
        LambdaQueryWrapper<BloodSampleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodSample.getBloodSampleId()),BloodSampleEntity::getBloodSampleId,bloodSample.getBloodSampleId())
                .eq(Strings.isNotBlank(bloodSample.getBloodId()),BloodSampleEntity::getBloodId,bloodSample.getBloodId())
                .eq(Strings.isNotBlank(bloodSample.getSampleStatus()),BloodSampleEntity::getSampleStatus,bloodSample.getSampleStatus())
                .eq(Strings.isNotBlank(bloodSample.getPlace()),BloodSampleEntity::getPlace,bloodSample.getPlace())
                .ge(Strings.isNotBlank(receivedStartTime),BloodSampleEntity::getReceivedTime,receivedStartTime)
                .le(Strings.isNotBlank(receivedEndTime), BloodSampleEntity::getReceivedTime,receivedEndTime)
                .eq(BloodSampleEntity::getIfShow, 1);


        IPage<BloodSampleEntity> page = this.page(
                new Query<BloodSampleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据血液id查询血样
     * @param bloodId
     * @return
     */
    @Override
    public BloodSampleEntity getByBloodId(String bloodId) {
        // 构建queryWrapper对象
        LambdaQueryWrapper<BloodSampleEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(bloodId), BloodSampleEntity::getBloodId, bloodId)
                    .eq(BloodSampleEntity::getIfShow, 1);
        // 查询
        return bloodSampleDao.selectOne(queryWrapper);
    }

    /**
     * 根据样本单号查询样本信息
     * @param bloodSampleId
     * @return
     */
    @Override
    public BloodSampleEntity getBySampleId(String bloodSampleId) {
        // 创建LambdaQueryWrapper对象
        LambdaQueryWrapper<BloodSampleEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(bloodSampleId), BloodSampleEntity::getBloodSampleId, bloodSampleId)
                    .eq(BloodSampleEntity::getIfShow, 1);
        // 返回查询结果
        return bloodSampleDao.selectOne(queryWrapper);
    }

    /**
     * 根据样本单号查询检测报告信息
     * @param bloodSampleId
     * @return
     */
    @Override
    public BloodSampleReportVo selectSampleReport(String bloodSampleId) {
        return bloodSampleDao.selectSampleReport(bloodSampleId);
    }

}